package com.healthyeating.manage.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.healthyeating.common.utils.DateUtils;
import com.healthyeating.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.healthyeating.manage.mapper.PhysicalMapper;
import com.healthyeating.manage.domain.Physical;
import com.healthyeating.manage.service.IPhysicalService;

/**
 * 身体管理Service业务层处理
 *
 * @author 段奇乐
 * @date 2025-03-20
 */
@Service
public class PhysicalServiceImpl implements IPhysicalService {

    @Autowired
    private PhysicalMapper physicalMapper;

    /**
     * 查询身体管理
     *
     * @param physicalId 身体管理主键
     * @return 身体管理
     */
    @Override
    public Physical selectPhysicalByPhysicalId(Long physicalId) {
        return physicalMapper.selectPhysicalByPhysicalId(physicalId);
    }

    /**
     * 查询身体管理列表
     *
     * @param physical 身体管理
     * @return 身体管理
     */
    @Override
    public List<Physical> selectPhysicalList(Physical physical) {
        if (physical.getCreateTime() != null) {
            Map<String, Object> map = DateUtils.extractTime(physical.getCreateTime());
            physical.setStartOfDay((Date) map.get("startOfDay"));
            physical.setEndOfDay((Date) map.get("endOfDay"));
        }
        physical.setUserId(SecurityUtils.getUserId());
        return physicalMapper.selectPhysicalList(physical);
    }

    /**
     * 新增身体管理
     *
     * @param physical 身体管理
     * @return 结果
     */
    @Override
    public int insertPhysical(Physical physical) {
        // 计算BMI
        Double weight = Double.valueOf(physical.getPhysicalWeight());
        Double height = Double.valueOf(physical.getPhysicalHeight());
        // 身高转换为米
        height = height / 100;
        // 计算结果保留两位小数
        physical.setPhysicalBmi(String.format("%.2f", weight / (height * height)));
        // 计算腰臀比
        Double waistline = Double.valueOf(physical.getPhysicalWaistline());
        Double hips = Double.valueOf(physical.getPhysicalHips());
        // 计算结果保留两位小数
        physical.setWaistHipRadio(String.format("%.2f", waistline / hips));
        physical.setCreateTime(DateUtils.getNowDate());
        physical.setUserId(SecurityUtils.getUserId());
        return physicalMapper.insertPhysical(physical);
    }

    /**
     * 修改身体管理
     *
     * @param physical 身体管理
     * @return 结果
     */
    @Override
    public int updatePhysical(Physical physical) {
        return physicalMapper.updatePhysical(physical);
    }

    /**
     * 批量删除身体管理
     *
     * @param physicalIds 需要删除的身体管理主键
     * @return 结果
     */
    @Override
    public int deletePhysicalByPhysicalIds(Long[] physicalIds) {
        return physicalMapper.deletePhysicalByPhysicalIds(physicalIds);
    }

    /**
     * 删除身体管理信息
     *
     * @param physicalId 身体管理主键
     * @return 结果
     */
    @Override
    public int deletePhysicalByPhysicalId(Long physicalId) {
        return physicalMapper.deletePhysicalByPhysicalId(physicalId);
    }
}
